package com.controleur.listener.impl;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import com.main.EasyLunch;
import com.modele.dao.impl.RestaurantDAO;
import com.modele.entity.Restaurant;
import com.modele.entity.TypeRestaurant;
import com.vue.Fenetre;
import com.vue.RestaurantListeVue;
import com.vue.RestaurantModifierVue;

public class RestaurantModifierListener implements ActionListener{
	
	public Fenetre fenetre;
	public Restaurant resto;
	
	public RestaurantModifierListener(Fenetre fenetre){
		this.fenetre=fenetre;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
	   String s = e.getActionCommand();
       if (s.equals("Sauvegarder")) {

    	    updateRestaurant();
	      	this.fenetre.setPanneau(new RestaurantListeVue(fenetre));
	   } 
       
		
	}
	public Restaurant updateRestaurant(){
		RestaurantDAO dao = (RestaurantDAO)EasyLunch.getFactory().getRestaurantDAO();
		RestaurantModifierVue form = (RestaurantModifierVue)fenetre.getPanneauApplication();
		int type = dao.findIdTypeByName(form.getType().getSelectedItem().toString());
		System.out.println("restau "+form.getRestaurant().getId());
		resto = form.getRestaurant();
		int intType = dao.findIdTypeByName(form.getType().getSelectedItem().toString());
		
		resto.setNom(form.getNom().getText());
				resto.setTypeRestaurant(new TypeRestaurant(intType, form.getType().getSelectedItem().toString()));
				resto.setRue(form.getRue().getText()); 
				resto.setNum(form.getNumero().getText());
				resto.setVille(form.getVille().getText());
				resto.setCp(form.getCp().getText());
				resto.setDescription(form.getDesc().getText());
				resto.setTel(form.getTel().getText());
				resto.setSite(form.getSite().getText());
		dao.update(resto);
		return resto;
	}
		
	
}
